﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.EntityContext;

namespace GestionPedago.Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de l'Entity ModuleParECF
    /// </summary>
    public abstract class ModuleParECFOPManager
    {
        /// <summary>
        /// Méthode de récupération d'une liste de ModuleParECF
        /// </summary>
        /// <param name="recherche">Filtre de recherche</param>
        /// <param name="orderByLibelleAsc">Vrai pour un tri Ascendant</param>
        /// <returns>La liste des ModuleParECF</returns>
        public static List<ModuleParECF> GetListeModuleParECF(string recherche = null, bool orderByLibelleAsc = true)
        {
            try
            {
                IQueryable<ModuleParECF> query = EntityConnection.GetContext().ModuleParECF;

                query.Where(it => (recherche == null || recherche == String.Empty || it.Module.Libelle.ToUpper().Contains(recherche.ToUpper())));

                if (orderByLibelleAsc)
                {
                    query = query.OrderBy(it => it.Module.Libelle);
                }
                else
                {
                    query = query.OrderByDescending(it => it.Module.Libelle);
                }
                /*query.Select(it => it);*/

                List<ModuleParECF> lst = query.ToList();

                return lst;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Méthode de récupération d'un ModuleParECF par son identifiant
        /// </summary>
        /// <param name="id">Identifiant du ModuleParECF</param>
        /// <returns>L'objet ModuleParECF</returns>
        public static ModuleParECF GetModuleParECFById(int id)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().ModuleParECF
                            where it.IdModuleParECF == id
                            select it;

                ModuleParECF m = null;

                m = query.First();

                return m;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// Méthode de récupération d'un ModuleParECF par son idModule et idECF
        /// </summary>
        /// <param name="idModule">Identifiant du module</param>
        /// <param name="idECF">Identifiant de l'ECF</param>
        /// <returns>L'objet ModuleParECF</returns>
        public static ModuleParECF GetModuleParECFByIdModuleIdECF(int idModule, int idECF)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().ModuleParECF
                            where it.IdModule == idModule && it.IdECF == idECF
                            select it;

                ModuleParECF m = null;

                m = query.First();

                return m;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// Méthode d'ajout d'un objet ModuleParECF en base
        /// </summary>
        /// <param name="mpe">L'objet ModuleParECF à ajouter</param>
        /// <returns>L'objet ModuleParECF</returns>
        public static ModuleParECF AjouterModuleParECF(ModuleParECF mpe)
        {
            try
            {
                if (!EntityStateChecker.IsDetached(mpe))
                {
                    EntityConnection.GetContext().ModuleParECF.Detach(mpe);
                }
                EntityConnection.GetContext().ModuleParECF.AddObject(mpe);
                EntityConnection.GetContext().SaveChanges();
                return mpe;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    
        /// <summary>
        /// Méthode de suppression d'un ModuleParECF
        /// </summary>
        /// <param name="mpe">L'objet ModuleParECF à supprimer</param>
        public static void SupprimerModuleParECF(ModuleParECF mpe)
        {
            try
            {
                EntityConnection.GetContext().ModuleParECF.DeleteObject(mpe);
                EntityConnection.GetContext().SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
